import {
	seacrhWarehousing,
	doAudit,
	updateAssetHandle,
	getAssetHandle,
	checkDisposalOrderNo,
	checkDisposalOrderNoByUpdate
} from "@/views/wy-asset-management/disposal-audit/api/disposal-audit.js";
import assetDictionary from "@/views/wy-asset-management/mixin/assetDictionary";
import { mapGetters } from "vuex";
import { getDict } from "~/utils";
export default {
	mixins: [assetDictionary],
	data() {
		/**
		 * async / await 同步
		 * 校验处置单号的唯一性
		 */
		let validatorDisposalOrderNo = async (rule, value, callback) => {
			if (value == "") {
				callback(new Error("请输入处置单号"));
			}
			if (this.$route.query.assetHandleId == null) {
				//新增验证
				await checkDisposalOrderNo(value).then(respData => {
					let result = respData.data.data;
					if (result == false) {
						callback(new Error("处置单号重复"));
					}
				});
			} else {
				//修改验证
				await checkDisposalOrderNoByUpdate(
					value,
					this.$route.query.assetHandleId
				).then(respData => {
					let result = respData.data.data;
					if (result == false) {
						callback(new Error("处置单号重复"));
					}
				});
			}
		};

		return {
			activeName: "处置审核信息",
			// 是否是政府项目
			government: 0,
			loading: true,

			//上传文件相关
			busId: this.$route.query.assetFixedWarehousingId,
			modelType: "asset-warehousing",
			url: "/admin/sys-file/upLodeFileEdit",
			fileList: [],

			changeBusId: "",
			changeFileList: [],

			//弹出框 初始时隐藏
			dialogVisible: false,

			//入库信息
			formObj: {
				//基本信息
				assetFixedWarehousingId: this.$route.query.assetFixedWarehousingId,
				roomId: this.$route.query.roomId,
				assetName: "",
				assetClassification: 0,
				useStatus: 0,
				acquisitionMethod: 0,
				acquisitionDate: "",
				investmentAcquisitionDate: "",
				valueType: 0,
				registrationPrice: 0,
				registrationFee: 0,
				taxation: 0,
				value: 0,
				number: "",
				measurementUnit: 0,
				landArea: 0,
				propertyRightForm: 0,
				ownershipCertificateNo: "",
				issueData: "",
				ownershipPeriod: "",
				ownershipCertificate: "",
				location: "",
				ownershipNature: 0,
				selfUseArea: 0,
				selfUseValue: 0,
				buildingStructure: 0,
				voucherNo: "",
				voucherDateStart: "",
				voucherDateEnd: ""
			},

			//处置信息
			formObj2: {
				assetHandleId: this.$route.query.assetHandleId,
				assetFixedWarehousingId: this.$route.query.assetFixedWarehousingId,
				voucherNo: "",
				voucherDateStart: "",
				voucherDateEnd: "",
				disposalOrderNo: "",
				disposalType: 0,
				residualIncome: 0,
				disposalReason: "",
				handleRemarks: "",
				approveContent: "同意",
				assetChangeRecordVoList: [],
				//审核状态 0 不通过 1 通过
				changeType: 1,
				changeId: ""
			}
			// rules2: {
			//     disposalOrderNo: [
			//         {
			//             required: true,
			//             message: "请输入处置单号",
			//             trigger: "blur"
			//         },
			//         { validator: validatorDisposalOrderNo, trigger: "blur" }
			//     ],
			//     disposalType: [
			//         {
			//             required: true,
			//             message: "请输入处置类型",
			//             trigger: "blur"
			//         }
			//     ],

			//     //校验金额或者面积
			//     residualIncome: [
			//         {
			//             required: true,
			//             message: "请输入残值收入",
			//             trigger: "change"
			//         },
			//         {
			//             pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
			//             message: "请输入合法的数字，最多两位小数",
			//             trigger: "change"
			//         }
			//     ],

			//     disposalReason: [
			//         {
			//             required: true,
			//             message: "请输入处置原因",
			//             trigger: "blur"
			//         }
			//     ]
			// }
		};
	},
	computed: {
		...mapGetters({
			cityAssembly: "getCityAssembly",
			districtCollection: "getDistrictCollection"
		})
	},
	/**
	 * 载入事件
	 */
	mounted() {
		//根据主键查询已入库的信息
		this.getWarehousing();
		//获取处置信息
		if (this.$route.query.assetHandleId != null) {
			this.getHandle();
		}
	},
	methods: {
		// 获取字典值列表
		getDict(v) {
			return getDict(v);
		},
		/**
		 * 根据主键查询已入库的信息
		 */
		getWarehousing() {
			this.loading = true;
			seacrhWarehousing(this.$route.query.assetFixedWarehousingId)
				.then(resp => {
					let obj = resp.data.data;
					this.formObj = obj;
					this.government = this.formObj.assetRegistrationId == "1" ? "1" : "0";
					this.formObj2.voucherNo = obj.voucherNo;
					this.formObj2.voucherDateStart = obj.voucherDateStart;
					this.formObj2.voucherDateEnd = obj.voucherDateEnd;
					this.loading = false;
				})
				.catch(error => {
					this.$message({
						type: "error",
						message: "获取物业入库信息异常"
					});
				})
				.finally(() => (this.loading = false));
		},

		/**
		 * 根据主键查询处置的信息
		 */
		getHandle() {
			this.loading = true;
			getAssetHandle(this.$route.query.assetHandleId)
				.then(resp => {
					let obj = resp.data.data;
					this.formObj2 = obj;
					// this.formObj2.voucherNo = obj.voucherNo;
					// this.formObj2.approveContent = obj.approveContent
					//     ? obj.approveContent
					//     : "同意";
					// this.formObj2.voucherDateStart = obj.voucherDateStart;
					// this.formObj2.voucherDateEnd = obj.voucherDateEnd;
					this.loading = false;
				})
				.catch(error => {
					this.$message({
						type: "error",
						message: "获取处置信息异常"
					});
				})
				.finally(() => (this.loading = false));
		},

		// 表单提交事件
		sendData(val) {
			this.loading = true;
			this.formObj2.changeType = val;
			if (!this.formObj2.approveContent)
				this.formObj2.approveContent = val == 0 ? "不同意" : "同意";
			this.$refs.handleFile.submit(this.modelType);
		},

		//  * 保存表单信息
		saveEntity(val) {
			if (null != val) {
				this.formObj2.changeId = val.data;
			}
			doAudit(this.formObj2)
				.then(data => {
					this.$message({
						type: "success",
						message: "操作成功"
					});
					//操作完成 跳转页面
					this.$router.push("/asset-management/disposal-audit-list");
				})
				.catch(error => {
					this.$message({
						type: "error",
						message: "操作失败"
					});
				})
				.finally(() => (this.loading = false));
		},

		//返回
		goBack() {
			this.$router.push("/asset-management/disposal-audit-list");
		}
	}
};
